package com.ruoyi.web.controller.mes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.mapper.MesLingCompareMapper;
import com.ruoyi.web.service.MesLingCompareService;
import com.ruoyi.web.vo.MesLingCompare;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/mes/lingCompare")
public class MesLingCompareController extends BaseController {
    @Resource
    private MesLingCompareService mesLingCompareService;
    @Resource
    private MesLingCompareMapper mesLingCompareMapper;

    /**
     * 查询实际用纸令数
     * @Date: 2024年7月25日
     * @Auth: 张恒
     */
    @GetMapping("/list")
    public TableDataInfo list(MesLingCompare compare){
        if(compare.getEndTime() != null){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(compare.getEndTime());
            try {
                Date parse = dateFormat.parse(format + " 23:59:59");
                compare.setEndTime(parse);
            } catch (ParseException e) {
                throw new BaseException("截至日期转换出错：" + e.getMessage());
            }
        }
        // 查询合计
        MesLingCompare sumLing = mesLingCompareMapper.selectSumLing(compare);
        HashMap<String, String> hashMap = new HashMap<>();
        if(sumLing != null){
            hashMap.put("expectLing", sumLing.getExpectLing());
            hashMap.put("realityLing", sumLing.getRealityLing());
        }
        startPage();
        // 修改此sql时，记得同步修改合计sql
        List<MesLingCompare> list = mesLingCompareService.list(new QueryWrapper<MesLingCompare>()
                .like(StringUtils.isNotEmpty(compare.getWorkshopName()), "workshop_name", compare.getWorkshopName())
                .like(StringUtils.isNotEmpty(compare.getMachineName()), "machine_name", compare.getMachineName())
                .like(StringUtils.isNotEmpty(compare.getReportPeople()), "report_people", compare.getReportPeople())
                .ge(compare.getBeginTime() != null, "create_time", compare.getBeginTime())
                .le(compare.getEndTime() != null, "create_time", compare.getEndTime())
                .orderByAsc("pid")
        );
        if(list.size() > 0){
            list.get(0).setHashMap(hashMap);
        }
        return getDataTable(list);
    }
}
